package com.ruoyi.gd.ywdwgdHistory.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.gd.ywdwgdHistory.domain.bo.ZYwdwgdHistoryBo;
import com.ruoyi.gd.ywdwgdHistory.domain.vo.ZYwdwgdHistoryVo;
import com.ruoyi.gd.ywdwgdHistory.domain.ZYwdwgdHistory;
import com.ruoyi.gd.ywdwgdHistory.mapper.ZYwdwgdHistoryMapper;
import com.ruoyi.gd.ywdwgdHistory.service.IZYwdwgdHistoryService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 业务完工单历史数据Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-08
 */
@RequiredArgsConstructor
@Service
public class ZYwdwgdHistoryServiceImpl implements IZYwdwgdHistoryService {

    private final ZYwdwgdHistoryMapper baseMapper;

    /**
     * 查询业务完工单历史数据
     */
    @Override
    public ZYwdwgdHistoryVo queryById(String id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询业务完工单历史数据列表
     */
    @Override
    public TableDataInfo<ZYwdwgdHistoryVo> queryPageList(ZYwdwgdHistoryBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ZYwdwgdHistory> lqw = buildQueryWrapper(bo);
        Page<ZYwdwgdHistoryVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询业务完工单历史数据列表
     */
    @Override
    public List<ZYwdwgdHistoryVo> queryList(ZYwdwgdHistoryBo bo) {
        LambdaQueryWrapper<ZYwdwgdHistory> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ZYwdwgdHistory> buildQueryWrapper(ZYwdwgdHistoryBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ZYwdwgdHistory> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getBjdh()), ZYwdwgdHistory::getBjdh, bo.getBjdh());
        lqw.eq(bo.getBjrq() != null, ZYwdwgdHistory::getBjrq, bo.getBjrq());
        lqw.eq(StringUtils.isNotBlank(bo.getKhid()), ZYwdwgdHistory::getKhid, bo.getKhid());
        lqw.like(StringUtils.isNotBlank(bo.getKhname()), ZYwdwgdHistory::getKhname, bo.getKhname());
        lqw.eq(StringUtils.isNotBlank(bo.getKhsh()), ZYwdwgdHistory::getKhsh, bo.getKhsh());
        lqw.eq(StringUtils.isNotBlank(bo.getKhdz()), ZYwdwgdHistory::getKhdz, bo.getKhdz());
        lqw.eq(StringUtils.isNotBlank(bo.getYsfj()), ZYwdwgdHistory::getYsfj, bo.getYsfj());
        lqw.eq(StringUtils.isNotBlank(bo.getBjdfj()), ZYwdwgdHistory::getBjdfj, bo.getBjdfj());
        lqw.eq(StringUtils.isNotBlank(bo.getBjdfjpdf()), ZYwdwgdHistory::getBjdfjpdf, bo.getBjdfjpdf());
        lqw.eq(bo.getHtje() != null, ZYwdwgdHistory::getHtje, bo.getHtje());
        lqw.eq(StringUtils.isNotBlank(bo.getWgdh()), ZYwdwgdHistory::getWgdh, bo.getWgdh());
        lqw.eq(bo.getWgrq() != null, ZYwdwgdHistory::getWgrq, bo.getWgrq());
        lqw.eq(bo.getZjfs() != null, ZYwdwgdHistory::getZjfs, bo.getZjfs());
        lqw.eq(StringUtils.isNotBlank(bo.getWgdfj()), ZYwdwgdHistory::getWgdfj, bo.getWgdfj());
        lqw.eq(StringUtils.isNotBlank(bo.getWgdfjpdf()), ZYwdwgdHistory::getWgdfjpdf, bo.getWgdfjpdf());
        lqw.eq(bo.getXcrq() != null, ZYwdwgdHistory::getXcrq, bo.getXcrq());
        lqw.eq(StringUtils.isNotBlank(bo.getXcfzr()), ZYwdwgdHistory::getXcfzr, bo.getXcfzr());
        lqw.like(StringUtils.isNotBlank(bo.getXcfzrname()), ZYwdwgdHistory::getXcfzrname, bo.getXcfzrname());
        lqw.eq(StringUtils.isNotBlank(bo.getXcry()), ZYwdwgdHistory::getXcry, bo.getXcry());
        lqw.like(StringUtils.isNotBlank(bo.getXcryname()), ZYwdwgdHistory::getXcryname, bo.getXcryname());
        lqw.eq(StringUtils.isNotBlank(bo.getXclx()), ZYwdwgdHistory::getXclx, bo.getXclx());
        lqw.eq(StringUtils.isNotBlank(bo.getXcbz()), ZYwdwgdHistory::getXcbz, bo.getXcbz());
        lqw.eq(StringUtils.isNotBlank(bo.getXctzyh()), ZYwdwgdHistory::getXctzyh, bo.getXctzyh());
        lqw.eq(StringUtils.isNotBlank(bo.getSfsqdzd()), ZYwdwgdHistory::getSfsqdzd, bo.getSfsqdzd());
        lqw.eq(StringUtils.isNotBlank(bo.getSfgkhdzd()), ZYwdwgdHistory::getSfgkhdzd, bo.getSfgkhdzd());
        lqw.eq(StringUtils.isNotBlank(bo.getZzdy()), ZYwdwgdHistory::getZzdy, bo.getZzdy());
        lqw.eq(StringUtils.isNotBlank(bo.getYyjkh()), ZYwdwgdHistory::getYyjkh, bo.getYyjkh());
        lqw.eq(StringUtils.isNotBlank(bo.getSfkp()), ZYwdwgdHistory::getSfkp, bo.getSfkp());
        lqw.eq(bo.getKpje() != null, ZYwdwgdHistory::getKpje, bo.getKpje());
        lqw.eq(StringUtils.isNotBlank(bo.getFph()), ZYwdwgdHistory::getFph, bo.getFph());
        lqw.eq(bo.getKprq() != null, ZYwdwgdHistory::getKprq, bo.getKprq());
        lqw.eq(bo.getXzkpsj() != null, ZYwdwgdHistory::getXzkpsj, bo.getXzkpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getYhsfqr()), ZYwdwgdHistory::getYhsfqr, bo.getYhsfqr());
        lqw.eq(StringUtils.isNotBlank(bo.getTscl()), ZYwdwgdHistory::getTscl, bo.getTscl());
        lqw.eq(StringUtils.isNotBlank(bo.getWbqk()), ZYwdwgdHistory::getWbqk, bo.getWbqk());
        lqw.eq(StringUtils.isNotBlank(bo.getSfhk()), ZYwdwgdHistory::getSfhk, bo.getSfhk());
        lqw.eq(StringUtils.isNotBlank(bo.getDzzh()), ZYwdwgdHistory::getDzzh, bo.getDzzh());
        lqw.eq(StringUtils.isNotBlank(bo.getSsgs()), ZYwdwgdHistory::getSsgs, bo.getSsgs());
        lqw.eq(StringUtils.isNotBlank(bo.getBz()), ZYwdwgdHistory::getBz, bo.getBz());
        lqw.eq(StringUtils.isNotBlank(bo.getJcfs()), ZYwdwgdHistory::getJcfs, bo.getJcfs());
        lqw.eq(StringUtils.isNotBlank(bo.getXczt()), ZYwdwgdHistory::getXczt, bo.getXczt());
        lqw.eq(StringUtils.isNotBlank(bo.getLsbjdid()), ZYwdwgdHistory::getLsbjdid, bo.getLsbjdid());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateId()), ZYwdwgdHistory::getCreateId, bo.getCreateId());
        return lqw;
    }

    /**
     * 新增业务完工单历史数据
     */
    @Override
    public Boolean insertByBo(ZYwdwgdHistoryBo bo) {
        ZYwdwgdHistory add = BeanUtil.toBean(bo, ZYwdwgdHistory.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    @Override
    public Boolean insert(ZYwdwgdHistory history) {
        boolean flag = baseMapper.insert(history) > 0;
        return flag;
    }

    /**
     * 修改业务完工单历史数据
     */
    @Override
    public Boolean updateByBo(ZYwdwgdHistoryBo bo) {
        ZYwdwgdHistory update = BeanUtil.toBean(bo, ZYwdwgdHistory.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ZYwdwgdHistory entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除业务完工单历史数据
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
